<?php

/**
 * Project form base class.
 *
 * @package    form
 * @version    SVN: $Id: sfDoctrineFormBaseTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
abstract class BaseFormDoctrine extends sfFormDoctrine
{
  protected static
    $formats = array
               (
                 'integer' => array
                            (
                              'size'        => 12, 
                              'style'       => 'text-align: right', 
                              'onBlur'      => 'formatInput(this)', 
                              'patternType' => 'integer', 
                            ),
                 'float' => array
                            (
                              'size'        => 12, 
                              'style'       => 'text-align: right', 
                              'onBlur'      => 'formatInput(this)', 
                              'patternType' => 'float', 
                              'decimals'    => 2, 
                              'dec_point'   => '.'
                            ),
                 'currency' => array
                            (
                              'size'        => 12, 
                              'style'       => 'text-align: right', 
                              'onBlur'      => 'formatInput(this)', 
                              'patternType' => 'currency', 
                              'decimals'    => 2, 
                              'dec_point'   => '.'
                            ),
                 'string15' => array
                            (
                              'maxlength'   => 15, 
                              'onBlur'      => 'formatInput(this)', 
                              'patternType' => 'string15'
                            ),
                 'string20' => array
                            (
                              'maxlength'   => 20, 
                              'onBlur'      => 'formatInput(this)', 
                              'patternType' => 'string20'
                            ),
                 'string25' => array
                            (
                              'maxlength'   => 25, 
                              'onBlur'      => 'formatInput(this)', 
                              'patternType' => 'string25'
                            ),
                 'string30' => array
                            (
                              'maxlength'   => 30, 
                              'onBlur'      => 'formatInput(this)', 
                              'patternType' => 'string30'
                            ),
                 'string60' => array
                            (
                              'maxlength'   => 60, 
                              'onBlur'      => 'formatInput(this)', 
                              'patternType' => 'string60'
                            ),
                 'nothing'  => array
                            (
                              'oncontextmenu' => 'return false;', 
                              'ondrop'        => 'return false;', 
                              'ondrag'        => 'return false;', 
                              'oncopy'        => 'return false;', 
                              'onpaste'       => 'return false;'
                            ),
               );
               
  public function setup()
  {
  }
  
  public function getCamelCaseName()
  {
    return $this->getModelName();
  }
  public function getTable()
  {
    return Doctrine::getTable($this->getModelName());
  }
  
  public function updateObject($values = null)
  {
    if (is_null($values))
    {
      $values = $this->values;
    }

    $values = $this->processValues($values);
    $values = $this->fixValues($values);
    $this->object->fromArray($values);
    // embedded forms
    //$this->checkEmbeddedForms($values);
    $this->updateObjectEmbeddedForms($values);
    return $this->object;
  }
  
  protected function doSave($con = null)
  {
    if (is_null($con))
    {
      $con = $this->getConnection();
    }
    $this->updateObject();
    $this->object->save($con);

    // embedded forms
    $this->saveEmbeddedForms($con);
    
    // Ticket #6432
    // just in tests, when calling save in a many to many relationship, the object
    // does not reload automatically its children. 
    $this->object->refreshRelated();
  }
  protected function fixValues($values)
  {
    return $values;
  }
  
  protected function checkEmbeddedForms($values, $form = null)
  {
    if (is_null($form))
    {
      $form = $this;
    }
    
    $clone = $form; //clone $form; strange object reseting when cloning
    $to_validate = false;
    foreach ($clone as $name => $value)
    {
      $embeddedFormKeys = array_keys($form->getEmbeddedForms());
      if (!in_array($name, $embeddedFormKeys))
      {
        $to_validate = ($name != 'id' && $name != 'page_id' ? !empty($values[$name]) : false) || $to_validate;
      }
      else
      {
        $embeddedForms = $form->getEmbeddedForms();
        $to_validate_form = $this->checkEmbeddedForms($values[$name], $embeddedForms[$name]);
        
        if (!$to_validate_form)
        {
          $object = $this->getInnerObject($embeddedForms[$name]);
          if ($object && !$object->isNew())
          {
            $object->delete();
          }
          unset($form[$name]);
        }
        
        $to_validate = $to_validate || $to_validate_form;
      }
    }
    
    return $to_validate;
  }
  
  protected function getInnerObject(sfForm $form)
  {
    $object = null;
    if ($form instanceof sfFormDoctrine)
    {
      $object = $form->getObject();
    }
    else
    {
      /*foreach ($form->getEmbeddedForms() as $eform)
      {
        $object = $this->getInnerObject($eform);
      }*/
    }
    
    return $object;
  }
  
  
  public function radioFormatter($widget, $inputs)
  {
    $rows = array();
    foreach ($inputs as $input)
    {
      $rows[] = $input['input'].$this->getOption('label_separator').'&nbsp;'.$input['label'];
    }
    
    return implode('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $rows);
  }
  public function getStandardDateFormat()
  {
    return '%year%&nbsp;-&nbsp;%month%&nbsp;-&nbsp;%day%';
  }
  public function getStandardTimeFormat()
  {
    return '&nbsp;%hour%:%minute%';
  }
}